//
//  TKYAddImageOrVideoView.m
//  StationHouseHelper
//
//  Created by apple on 2018/7/26.
//  Copyright © 2018年 铁科院. All rights reserved.
//

#import "TKYAddImageOrVideoView.h"
#import "TKYPhotoOrVideoCell.h"
@interface TKYAddImageOrVideoView()

@property (nonatomic, strong) UIView *bottomLineView;

@end
@implementation TKYAddImageOrVideoView
-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]){
        [self addSubview:self.titleLabel];
        [self addSubview:self.cameraButton];
        [self addSubview:self.collectionView];
        [self addSubview:self.bottomLineView];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(kWidthScale*10);
            make.top.equalTo(self).with.offset(0);
            make.width.mas_equalTo(kWidthScale*100);
            make.height.mas_equalTo(kHeightScale *20);
        }];
        
        [self.cameraButton mas_makeConstraints:^(MASConstraintMaker *make) {
            //make.left.equalTo(self.contentView).with.offset(20);
            make.top.equalTo(self).with.offset(kHeightScale *5);
            make.right.equalTo(self).with.offset(-kWidthScale *10);
            make.height.mas_equalTo(kHeightScale *20);
            make.width.mas_equalTo(kWidthScale *60);
        }];
        
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(kWidthScale *20);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(kHeightScale *10);
            make.right.equalTo(self).with.offset(-kWidthScale *10);
            make.bottom.equalTo(self).offset(-kHeightScale *10);
            make.height.mas_equalTo(kHeightScale *100);
        }];
        
        [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(kWidthScale*10);
            make.right.equalTo(self).with.offset(-kWidthScale*10);
            make.bottom.equalTo(self).with.offset(-kHeightScale *1);
            make.height.mas_equalTo(kWidthScale*1);
        }];
    }
    return self;
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imageArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TKYPhotoOrVideoCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:kPhotoOrVideoCellID forIndexPath:indexPath];
    //cell.backgroundColor = [UIColor redColor];
    NSDictionary *dict = self.imageArr[indexPath.row];
    NSLog(@"%@",dict);
    if ([[dict objectForKey:@"GQIsImageURL"]integerValue]== 1) {
        cell.playButton.hidden = YES;
    }else{
        cell.playButton.hidden = NO;
    }
    __weak typeof(self) weakSelf = self;
    cell.deleteImageBlock = ^(UIButton *button) {
        [weakSelf.imageArr removeObject:dict];
        [weakSelf.collectionView reloadData];
    };
    cell.imageView.image = [dict objectForKey:@"image"];
    return cell;
}

- (UILabel *)titleLabel{
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.text = @"照片或视频";
        _titleLabel.font = Font(12);
        // _titleLabel.backgroundColor = [UIColor redColor];
    }
    return _titleLabel;
}

- (UIView *)bottomLineView{
    if (_bottomLineView == nil) {
        _bottomLineView = [[UIView alloc]init];
        _bottomLineView.backgroundColor = kMainColour;
    }
    return _bottomLineView;
}

- (UIButton *)cameraButton{
    if (_cameraButton == nil) {
        _cameraButton = [[UIButton alloc]init];
        _cameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cameraButton setImage:[UIImage imageNamed:@"add_photo"] forState:UIControlStateNormal];
        
        _cameraButton.titleLabel.font = Font(12);
        [_cameraButton setTitleColor:KNavColor forState:UIControlStateNormal];
        [_cameraButton addTarget:self action:@selector(addCameraAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cameraButton;
}


#pragma mark--懒加载
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.itemSize = CGSizeMake(kWidthScale *100, kWidthScale *100);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = 2;
       // layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        UICollectionView *collectionView;
        
        collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kHeightScale *100) collectionViewLayout:layout];
        collectionView.backgroundColor = kMainColour;
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.backgroundColor = [UIColor whiteColor];
        collectionView.bounces = YES;
        //注册cell
        [collectionView registerNib:[UINib nibWithNibName:@"TKYPhotoOrVideoCell" bundle:nil] forCellWithReuseIdentifier:kPhotoOrVideoCellID];
        collectionView.scrollsToTop = NO;
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView = collectionView;
    }
    return _collectionView;
}

- (void)addCameraAction:(UIButton *)button{
    if (self.addCameraBlock) {
        self.addCameraBlock(button);
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.seletetedCollectionCellBlock) {
        self.seletetedCollectionCellBlock(indexPath);
    }
}

@end
